import json
import tkinter as tk

import CreateQuestionnaireFrame
import WriteQuestionnaireFrame
from request_interface.req import show_create_qt_req
from utils.MAC import get_mac_address


class MainFrame(tk.Tk):
    def __init__(self):
        super().__init__()

        self.title("在线《Python程序设计》课程问卷调查系统")

        # 获取屏幕的宽和高
        sreen_width = self.winfo_screenwidth()
        screen_height = self.winfo_screenheight()

        # 设置窗口的宽和高
        root_width = int(sreen_width / 2)
        root_height = int(screen_height / 2)

        # 使窗口居中
        self.geometry("{}x{}+{}+{}".format(
            root_width, root_height,
            int((sreen_width - root_width) / 2), int((screen_height - root_height) / 2)
        ))

        self.create_button = tk.Button(self, text='创建', font=("宋体", 20),
                                       command=self.open_create_questionnaire_frame)
        self.create_button.place(relx=0.3, rely=0.2, relwidth=0.4, relheight=0.2)

        self.submit_button = tk.Button(self, text='填写', font=("宋体", 20),
                                       command=self.open_write_questionnaire_frame)
        self.submit_button.place(relx=0.3, rely=0.7, relwidth=0.4, relheight=0.2)

    # 我创建的
    def open_create_questionnaire_frame(self):
        self.withdraw()

        # 查询所有已经创建的
        data=[json.loads(v) for v in show_create_qt_req(get_mac_address())]

        create_questionnaire_frame = CreateQuestionnaireFrame.CreateQuestionnaireFrame(self,data)
        create_questionnaire_frame.mainloop()

    # 提交
    def open_write_questionnaire_frame(self):
        self.withdraw()
        write_questionnaire_frame = WriteQuestionnaireFrame.WriteQuestionnaireFrame(self)
        write_questionnaire_frame.mainloop()


if __name__ == '__main__':
    app = MainFrame()
    app.mainloop()